package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.dto.user.OmcUserQueryDto;
import com.csun.cmny.provider.model.dto.user.UserRoleDto;
import com.csun.cmny.provider.model.dto.volunteer.VolunteerQueryDto;
import com.csun.cmny.provider.model.service.*;
import com.csun.cmny.provider.model.vo.*;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.util.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-05-27 09:55
 * @Description: the class uac rpc service impl
 */
@Slf4j
@Service
public class UacRpcServiceImpl implements UacRpcService {

    @Resource
    private UacVolunteerFeignApi uacVolunteerFeignApi;
    @Resource
    private UacRoleFeignApi uacRoleFeignApi;
    @Resource
    private UacUserFeignApi uacUserFeignApi;
    @Resource
    private UacUserTokenFeignApi uacUserTokenFeignApi;
    @Resource
    private UacMerchantFeignApi uacMerchantFeignApi;
    @Resource
    private UacStaffFeignApi staffFeignApi;
    @Resource
    private UacGroupFeignApi uacGroupFeignApi;

    /**
     * 获取志愿者信息
     *
     * @param volunteerId 志愿者Id
     * @return 志愿者信息
     */
    @Override
    public VolunteerVo getVolunteerByVolunteerId(Long volunteerId) {

        try {
            Wrapper<VolunteerVo> wrapper = uacVolunteerFeignApi.getVolunteerByVolunteerId(volunteerId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018002);
            }

            VolunteerVo result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("getVolunteerByVolunteerId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * 更新志愿者积分
     *
     * @param volunteerId 志愿者Id
     * @param integral    积分
     * @return the int.
     */
    @Override
    public Integer updateVolunteerIntegralByVolunteerId(Long volunteerId, Integer integral) {

        try {
            Wrapper<Integer> wrapper = uacVolunteerFeignApi
                    .updateVolunteerIntegralByVolunteerId(volunteerId, integral);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018004);
            }

            Integer result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("updateVolunteerIntegralByVolunteerId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * 查询志愿者列表
     *
     * @param volunteerQueryDto volunteer query dto
     * @return the list.
     */
    @Override
    public List<VolunteerVo> queryVolunteerListWithPage(VolunteerQueryDto volunteerQueryDto) {

        try {
            Wrapper<List<VolunteerVo>> wrapper = uacVolunteerFeignApi
                    .queryVolunteerListWithPage(volunteerQueryDto);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018004);
            }

            List<VolunteerVo> result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryVolunteerListWithPage={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * 根据志愿者Id集合查询志愿者列表
     *
     * @param volunteerIdList volunteer id list
     * @return the list.
     */
    @Override
    public List<VolunteerVo> queryVolunteerListByVolunteerIds(List<Long> volunteerIdList) {

        try {
            Wrapper<List<VolunteerVo>> wrapper = uacVolunteerFeignApi
                    .queryVolunteerListByVolunteerIds(volunteerIdList);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018004);
            }

            List<VolunteerVo> result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryVolunteerListByVolunteerIds={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * 根据用户Id查询角色列表
     *
     * @param userId user id
     * @return
     */
    @Override
    public List<RpcRoleVo> queryRoleListByUserId(Long userId) {

        try {
            Wrapper<List<RpcRoleVo>> wrapper = uacRoleFeignApi.queryRoleListByUserId(userId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10013012);
            }

            List<RpcRoleVo> result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryRoleListByUserId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * 根据用户Id查询志愿者信息
     *
     * @param userId user id
     * @return
     */
    @Override
    public VolunteerVo getVolunteerByUserId(Long userId) {

        try {
            Wrapper<VolunteerVo> wrapper = uacVolunteerFeignApi.getVolunteerByUserId(userId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018002);
            }

            VolunteerVo result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("getVolunteerByUserId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * 根据用户Id查询用户信息
     *
     * @param userList user list
     * @return the list
     */
    @Override
    public List<RpcUserVo> queryUserListByUserIds(List<Long> userList) {

        try {
            Wrapper<List<RpcUserVo>> wrapper = uacUserFeignApi.queryUserListByUserIds(userList);
            if (null == wrapper) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018002);
            }

            List<RpcUserVo> result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryUserListByUserIds={}", e.getMessage(), e);
        }

        return null;
    }

    @Override
    public List<RpcMerchantVO> queryMerchantListByIdList(List<Long> userList) {
        try {
            Wrapper<List<RpcMerchantVO>> wrapper = uacUserFeignApi.queryMerchantListByIdList(userList);
            if (null == wrapper) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018002);
            }

            List<RpcMerchantVO> result = wrapper.getResult();
            assert result != null;
            return result;

        } catch (Exception e) {
            log.error("queryMerchantListByUserIds={}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public List<OmcUserVo> queryOmcUserListByUserIds(List<Long> userList) {
        try {
            Wrapper<List<OmcUserVo>> wrapper = uacUserFeignApi.queryOmcUserListByUserIds(userList);
            if (null == wrapper) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018002);
            }

            List<OmcUserVo> result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryOmcUserListByUserIds={}", e.getMessage(), e);
        }

        return null;
    }

    @Override
    public List<OmcUserVo> queryOmcUserListByQueryDto(OmcUserQueryDto omcUserQueryDto) {
        try {
            Wrapper<List<OmcUserVo>> wrapper = uacUserFeignApi.queryOmcUserListByQueryDto(omcUserQueryDto);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018002);
            }

            List<OmcUserVo> result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryOmcUserListByQueryDto={}", e.getMessage(), e);
        }

        return null;
    }

    @Override
    public List<OmcUserVo> queryOmcUnUnrsingUserListByUserIds(List<Long> userList, Long groupId, String roleCode) {
        try {
            Wrapper<List<OmcUserVo>> wrapper = uacUserFeignApi.queryOmcUnUnrsingUserListByUserIds(userList, groupId, roleCode);
            if (null == wrapper) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018002);
            }

            List<OmcUserVo> result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryOmcUnUnrsingUserListByUserIds={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * 根据角色和组织Id查询用户列表
     *
     * @param userRoleDto user role dto
     * @return
     */
    @Override
    public List<RpcUserVo> queryUserListByRoleCodeAndGroupId(UserRoleDto userRoleDto) {

        try {
            Wrapper<List<RpcUserVo>> wrapper = uacUserFeignApi.queryUserListByRoleCodeAndGroupId(userRoleDto);
            if (null == wrapper) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018002);
            }

            List<RpcUserVo> result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryUserListByRoleCodeAndGroupId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * rest user token by user id
     *
     * @param userId the user id
     */
    @Override
    public void restToken(Long userId) {
        try {
            Wrapper<Boolean> wrapper = uacUserTokenFeignApi.restToken(userId);

        } catch (Exception e) {
            log.error("queryUserListByRoleCodeAndGroupId={}", e.getMessage(), e);
        }

    }

    @Override
    public Integer checkMerchantOnline(Long merchantId) {

        try {
            Wrapper<Integer> wrapper = uacMerchantFeignApi.checkMerchantOnline(merchantId);
            if (null == wrapper) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018002);
            }

            Integer result = wrapper.getResult();
            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("checkMerchantOnline={}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public Integer checkServiceFirstLevelUsed(Long firstLevelId) {

        try {
            Wrapper<Integer> wrapper = uacMerchantFeignApi.checkServiceFirstLevelUsed(firstLevelId);
            if (null == wrapper) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018002);
            }

            Integer result = wrapper.getResult();
            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("checkServiceFirstLevelUsed={}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public Integer isStaff(Long staffId) {
        try {
            Wrapper<Integer> wrapper = uacMerchantFeignApi.countStaff(staffId);

            if (null == wrapper) {

                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {

                throw new OmcBizException(ErrorCodeEnum.OMC10080034);
            }


            return wrapper.getResult();
        } catch (Exception e) {
            log.error("checkServiceFirstLevelUsed={}", e.getMessage(), e);
        }

        return null;
    }

    @Override
    public Integer selectVolunteerCount(Long groupId) {
        try {
            Wrapper<Integer> wrapper = uacVolunteerFeignApi.selectVolunteerCount(groupId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018004);
            }

            Integer result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("selectVolunteerCount", e.getMessage(), e);
        }

        return null;
    }

    @Override
    public List<GroupInforVo> getAllGroup(Long groupId) {
        try {
            return uacGroupFeignApi.getAllGroup(groupId);
        } catch (Exception e) {
            log.error("getAllGroup", e.getMessage(), e);
        }

        return null;

    }

    /**
     * 查询该机构及子机构信息
     *
     * @param groupId   the group id
     *
     * @return  the rpc group vo list.
     */
    @Override
    public List<GroupVo> queryGroupListByGroupId(Long groupId) {

        try {
            Wrapper<List<GroupVo>> wrapper = uacGroupFeignApi.queryGroupListByGroupId(groupId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10013012);
            }

            List<GroupVo> result = wrapper.getResult();

            assert result != null;

            return result;

        } catch (Exception e) {
            log.error("queryGroupListByGroupId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * 查询该机构子机构信息
     *
     * @param groupId   the group id
     *
     * @return  the rpc group vo list.
     */
    @Override
    public List<GroupVo> queryChildListByGroupId(Long groupId) {

        try {
            Wrapper<List<GroupVo>> wrapper = uacGroupFeignApi.queryChildListByGroupId(groupId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10013012);
            }

            List<GroupVo> result = wrapper.getResult();

            assert result != null;

            return result;

        } catch (Exception e) {
            log.error("queryChildListByGroupId={}", e.getMessage(), e);
        }

        return null;
    }

    @Override
    public List<CountVo> selectType(Long groupId) {
        try {
            Wrapper<List<com.csun.cmny.provider.model.vo.CountVo>> wrapper = uacGroupFeignApi.selectType(groupId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException();
            }

            List<com.csun.cmny.provider.model.vo.CountVo> result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("selectType={}", e.getMessage(), e);
        }

        return null;
    }


}
